<?
/**
 * The file contains logger class for to support logging functionality in VC
 * 
 * @package	VC
 * @author	cryonyx
 */

class vc_logger {
	/**
	 * Contains path to the log file
	 *
	 * @var	string
	 */
	private $logFile;
	
	/**
	 * Identifies whether logger is in debug mode
	 *
	 * @var boolean
	 */
	private $debugMode;
	
	/**
	 * Identifies the level of logging, i.e. messages of what severity (at least) to write to the log
	 *
	 * @var integer
	 */
	private $logLevel;
	
	/**
	 * Identifies whether logger is enabled
	 *
	 * @var boolean
	 */
	private $enabled;
	
	/**
	 * Construction method of the class
	 *
	 * @param	string	$logFile	Path to log file (optional)
	 */
	public function __construct($logFile = NULL) {
		//	Setting path to log file
		$this->logFile	=	isset($logFile) ? $logFile : (dirname(__FILE__) . '/vc.log');
		
		//	Getting logger parameters from .xml file
		if(file_exists(dirname(__FILE__) . "/vc.logger.settings.xml")) {
			$xml	=	simplexml_load_file(dirname(__FILE__) . '/vc.logger.settings.xml');
			if($xml) {
				//	Parsing parameters from .xml file
				if(isset($xml->param)) {
					foreach($xml->param as $param) {
						$value	=	(string) $param;
						switch ((string) $param['name']) {
							//	Turns logger on or off
							case 'enabled':
								if(	(strtolower($value)	==	'yes')	||
									(strtolower($value)	==	'y')	||
									(strtolower($value)	==	'true')
								) {
									$this->enabled	=	TRUE;
								} else {
									$this->enabled	=	FALSE;
								}
								break;
							
							//	For debug mode, checking is it enabled or not
							case 'debugMode':
								if(	(strtolower($value)	==	'yes')	||
									(strtolower($value)	==	'y')	||
									(strtolower($value)	==	'true')
								) {
									$this->debugMode	=	TRUE;
								} else {
									$this->debugMode	=	FALSE;
								}
								break;
							
							//	Selecting the log level to use
							//	If debug mode is selected, it overrides these settings
							case 'logLevel':
								switch(strtolower($value)) {
									//	Log only errors
									case 'error':
										$this->logLevel	=	3;
										break;
									//	Log only errors and warnings
									case 'warn'	:
										$this->logLevel	=	2;
										break;
									//	Log all messages
									default		:
										$this->logLevel	=	0;
										break;
								}
								break;
								
							default:
								//	Just ignore it
								break;
						}
						
					}
				}
			}
		}
	}
	
	/**
	 * Returns path to the log file used
	 *
	 * @return string
	 */
	public function getLogFile() {
		return $this->logFile;
	}
	
	/**
	 * Sets log file to be used
	 *
	 * @param	string	$logFile	Path to log file
	 */
	public function setLogFile($logFile) {
		$this->logFile	=	$logFile;
	}
	
	/**
	 * Writes event to log
	 *
	 * @param	string	$message	The message to be logged
	 * @param	integer	$severity	Severity of the event
	 * @param	string	$where		The place (method, class, file and so on) where event has occured
	 */
	public function log($message, $severity, $where) {
		//	If logger is not enabled, just leave it
		if(!$this->enabled) {
			return;
		}

		//	If severity of the message it not sufficient and debug mode is not enabled,
		//	just leave it
		if(	!$this->debugMode	&&
			($severity < $this->logLevel)
		) {
			return;
		}
		
		$res = "[" . date("G:i:s, j.m.Y") . "]\t";

		switch($status) {				//	Creating different messages for different statuses
			case 3:						//	Error. Always logged
				$res .=	"[ERRO]";
				break;
			case 2:						//	Warning
				$res .=	"[WARN]";
				break;
			default:					//	Info
				$res .=	"[INFO]";
		}
		
		//	Appending place of the event and message itself
		$res .= "\t[" . $where . "]\t" . $message . "\n";
		
		if($logFile = @fopen($this->logFile, "a")) {
			$result = fwrite($logFile, $res);
			fclose($logFile);
		}
	}
}
?>